var app = getApp();
var api = require('./api.js');
var Vue = require('vue');
// var RongIMLib = require('@rongcloud/imlib-v4');
import TIM from '@tencentcloud/chat';
/**
 * request 普通请求
 */
// const TUICallKit = uni.requireNativePlugin('TencentCloud-TUICallKit');
let msg = {
  message: {}
};

function request(url, data = {}, method = "POST") {
  let _this = this;
  return new Promise(function(resolve, reject) {
    // uni.setStorageSync('token', '8b0e884f-31a7-4f79-bfb8-cd4df534439c');
    const token = uni.getStorageSync('user_info').token;
    // const token = 'c036664f-563e-4c39-82cc-dd4e312bad3d';

    data.token = token;
    uni.request({
      url: api.path + url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept-Language': 'zh-cn'
      },
      success(res) {
        console.log(res.data.code)
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else if (res.data.code == 401) {
          toLogin();
        } else if (res.data.msg) {
          _this.showToast(res.data.msg);
        }
      },
      fail(err) {
        // reject(err)
        _this.showToast('加载失败');
      }
    })
  });
}

function gamerequest(url, data = {}, method = "get") {
  let _this = this;
  return new Promise(function(resolve, reject) {
    // uni.setStorageSync('token', '8b0e884f-31a7-4f79-bfb8-cd4df534439c');
    const token = uni.getStorageSync('user_info').token;

    data.token = token;
    uni.request({
      url: api.apis + url,
      data: data,
      method: method,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Accept-Language': 'zh-cn'
      },
      success(res) {
        console.log(res.data.code)
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else if (res.data.code == 401) {
          toLogin();
        } else if (res.data.msg) {
          _this.showToast(res.data.msg);
        }
      },
      fail(err) {
        // reject(err)
        _this.showToast('加载失败');
      }
    })
  });
}

function setMsg(e) {
  msg.message = e;
  uni.showTabBarRedDot({
    index: 1,
  })
}

function loginim(e) {
  const userid = uni.getStorageSync('user_info').id
  const tim = TIM.create({
    SDKAppID: 1600052101
  });
  console.log(userid, e)
  // tim.setLogLevel(0); // 普通级别，日志量较多，接入时建议使用
  tim.setLogLevel(0); // release 级别，SDK 输出关键信息，生产环境时建议使用
  let promise = tim.login({
    userID: userid.toString(),
    userSig: e
  });
  promise.then(function(imResponse) {
    console.log(imResponse.data, '链接成功'); // 登录成功
    if (imResponse.data.repeatLogin === true) {
      // 标识帐号已登录，本次登录操作为重复登录。v2.5.1 起支持
      console.log(imResponse.data.errorInfo);
    }
  }).catch(function(imError) {
    console.warn('login error:', imError); // 登录失败的相关信息
  });
  let onMessageReceived = function(event) {
    // event.data - 存储 Message 对象的数组 - [Message]
    const messageList = event.data;
    messageList.forEach((message) => {
      console.log(message, 'xiaoxi1')
      setMsg(messageList)
      if (message.type === TIM.TYPES.MSG_TEXT) {

        console.log('文本消息')
        // 文本消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.TextPayload
      } else if (message.type === TIM.TYPES.MSG_IMAGE) {
        // 图片消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.ImagePayload
      } else if (message.type === TIM.TYPES.MSG_SOUND) {
        // 音频消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.AudioPayload
      } else if (message.type === TIM.TYPES.MSG_VIDEO) {
        // 视频消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.VideoPayload
      } else if (message.type === TIM.TYPES.MSG_FILE) {
        // 文件消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.FilePayload
      } else if (message.type === TIM.TYPES.MSG_CUSTOM) {
        // 自定义消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.CustomPayload
      } else if (message.type === TIM.TYPES.MSG_MERGER) {
        // 合并消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.MergerPayload
      } else if (message.type === TIM.TYPES.MSG_LOCATION) {
        // 地理位置消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.LocationPayload
      } else if (message.type === TIM.TYPES.MSG_GRP_TIP) {
        // 群提示消息 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.GroupTipPayload
      } else if (message.type === TIM.TYPES.MSG_GRP_SYS_NOTICE) {
        // 群系统通知 - https://web.sdk.qcloud.com/im/doc/zh-cn/Message.html#.GroupSystemNoticePayload
      }
    });
  };
  tim.on(TIM.EVENT.MESSAGE_RECEIVED, onMessageReceived);
}



/**
 * 获取用户信息
 */
function getUserInfo() {
  let that = this;
  return new Promise((resolve, reject) => {
    request(
      'user/index', {}, "POST"
    ).then(res => {
      console.log(res)
      resolve(res);
    })
  });
}

/**
 * 跳转到登录页面,并清空缓存
 */
function toLogin(e) {
  uni.hideLoading()
  uni.clearStorage();
  uni.clearStorageSync();
  uni.reLaunch({
    url: "/pages/index/index"
  })
}
/**
 * 对象转url参数
 * @param {Object} obj
 */
function objToUrl(obj) {
  let u = "";
  for (let key in obj) {
    u = u + key + "=" + obj[key] + "&";
  }
  return u.substring(0, u.length - 1);
}
/**
 * 消息提示框
 */
function showToast(msg = '', isback) {
  uni.showToast({
    title: msg,
    duration: 2000,
    icon: 'none'
  });
  if (isback) {
    setTimeout(function() {
      uni.navigateBack()
    }, 1000)
  }
}
/**
 *  弹出提示信息结束后执行方法
 */
function showMsg(msg, duration = 2000, callback) {
  uni.showToast({
    title: msg,
    icon: 'none',
    duration: duration,
    success: function() {
      callback
    }
  })
}

function seeimg(list, index) {
  if (list[0].indexOf('http') == -1) {
    for (let i = 0; i < list.length; i++) {
      list[i] = api.img + list[i];
      if (i == list.length - 1) {
        console.log(list)
        uni.previewImage({
          current: index,
          urls: list
        })
      }
    }
  } else {
    console.log(list)
    uni.previewImage({
      current: index,
      urls: list
    })
  }
}
/**
 *  页面跳转
 */
function urlTo(e) {
  uni.navigateTo({
    url: e
  })
}

/**
 *  页面跳转
 */
function urltabTo(e) {
  uni.switchTab({
    url: e
  })
}

/**
 *  页面返回
 */
function backTo(e, v) {
  uni.navigateBack({
    delta: e,
    duration: v
  })
}

/**
 *手机掩码
 * @param {Object} phone
 */
function phoneMask(phone) {
  return phone.substring(0, 3) + '****' + phone.substring(7);
}
/**
 * 验证手机号
 * @param {Object} phone
 */
function checkPhone(phone) {
  let zz = /^1[3456789]\d{9}$/;
  return zz.test(phone);
}
/**
 * 验证姓名
 * @param {Object} phone
 */
function checkName(name) {
  let zz = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/; //验证姓名正则
  return zz.test(name);
}
/*  
验证日期
*/
function checkDate(date) {
  let shengri_zz = /^\d{4}-\d{2}-\d{2}$/; //日期验证
  return shengri_zz.test(date);
}

/*  
格式化时间戳
*/
function timestampToTime(timestamp) {
  var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  var Y = date.getFullYear() + '-';
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
  // return Y + M + D+ h + m;
}
/* 
 判断是否微信浏览
 */
function isWeiXin() {
  var ua = window.navigator.userAgent.toLowerCase();
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true;
  } else {
    return false;
  }
}
/* 
		验证邮箱
	 */
function checkEmail(value) {
  return /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(value)
}
/* 
		身份证验证
	 */
function checkIDCard(idNum) {
  if (!idNum) {
    return false
  }
  // alert(idNum);
  var errors = new Array( // eslint-disable-line
    "alert('验证通过');",
    "alert('身份证号码位数不对');",
    "alert('身份证含有非法字符');",
    "alert('身份证号码校验错误');",
    "alert('身份证地区非法');"
  )
  // 身份号码位数及格式检验
  var re
  var len = idNum.length
  // 身份证位数检验
  if (len != 15 && len != 18) { // eslint-disable-line
    return false
  } else if (len == 15) { // eslint-disable-line
    re = new RegExp(/^(\d{6})()?(\d{2})(\d{2})(\d{2})(\d{3})$/)
  } else {
    re = new RegExp(/^(\d{6})()?(\d{4})(\d{2})(\d{2})(\d{3})([0-9xX])$/)
  }
  var area = {
    11: '北京',
    12: '天津',
    13: '河北',
    14: '山西',
    15: '内蒙古',
    21: '辽宁',
    22: '吉林',
    23: '黑龙江',
    31: '上海',
    32: '江苏',
    33: '浙江',
    34: '安徽',
    35: '福建',
    36: '江西',
    37: '山东',
    41: '河南',
    42: '湖北',
    43: '湖南',
    44: '广东',
    45: '广西',
    46: '海南',
    50: '重庆',
    51: '四川',
    52: '贵州',
    53: '云南',
    54: '西藏',
    61: '陕西',
    62: '甘肃',
    63: '青海',
    64: '宁夏',
    65: '新疆',
    71: '台湾',
    81: '香港',
    82: '澳门',
    91: '国外'
  }
  var idcard_array = new Array() // eslint-disable-line
  idcard_array = idNum.split('') // eslint-disable-line
  // 地区检验
  if (area[parseInt(idNum.substr(0, 2))] == null) {
    return false
  }
  // 出生日期正确性检验
  var a = idNum.match(re)
  if (a != null) { // eslint-disable-line
    var flag
    var DD
    if (len == 15) { // eslint-disable-line
      DD = new Date('19' + a[3] + '/' + a[4] + '/' + a[5])
      flag = DD.getYear() == a[3] && (DD.getMonth() + 1) == a[4] && DD.getDate() == a[5] // eslint-disable-line
    } else if (len == 18) { // eslint-disable-line
      DD = new Date(a[3] + '/' + a[4] + '/' + a[5])
      flag = DD.getFullYear() == a[3] && (DD.getMonth() + 1) == a[4] && DD.getDate() == a[
        5] // eslint-disable-line
    }
    if (!flag) {
      // return false;
      return false
    }
    // 检验校验位
    if (len == 18) { // eslint-disable-line
      var S = (parseInt(idcard_array[0]) + parseInt(idcard_array[10])) * 7 +
        (parseInt(idcard_array[1]) +
          parseInt(idcard_array[11])) * 9 +
        (parseInt(idcard_array[2]) +
          parseInt(idcard_array[12])) * 10 +
        (parseInt(idcard_array[3]) +
          parseInt(idcard_array[13])) * 5 +
        (parseInt(idcard_array[4]) +
          parseInt(idcard_array[14])) * 8 +
        (parseInt(idcard_array[5]) +
          parseInt(idcard_array[15])) * 4 +
        (parseInt(idcard_array[6]) +
          parseInt(idcard_array[16])) * 2 +
        parseInt(idcard_array[7]) * 1 +
        parseInt(idcard_array[8]) * 6 +
        parseInt(idcard_array[9]) * 3
      var Y = S % 11
      var M = 'F'
      var JYM = '10X98765432'
      M = JYM.substr(Y, 1) // 判断校验位
      // 检测ID的校验位
      if (M == idcard_array[17]) { // eslint-disable-line
        return true
        // return "";
      } else {
        // return false;
        return false
      }
    }
  } else {
    // return false;
    return false
  }
  return true
}
/* 
 检查更新
 */
function getVersion(e) {
  let _this = this;
  _this.request('common/version', {
    version: api.versions
  }, 'POST').then(res => {
    if (res.android.newversion != api.versions) {
      uni.showModal({
        title: '更新提示',
        content: '为了您的正常使用请您更新最新版本',
        showCancel: false,
        confirmText: '立即更新',
        success: function(gdx) {
          if (gdx.confirm) {
            console.log(res.android.downloadurl)
            plus.runtime.openURL(res.android.downloadurl);
          }
        }
      });
    }
  })
}
/* 
 微信,支付宝 app支付调取
 */
function pay(type, data, order_id, deltype) {
  return new Promise(function(resolve, reject) {
    uni.requestPayment({
      provider: type,
      timeStamp: data.timeStamp,
      nonceStr: data.nonceStr,
      package: data.package,
      signType: data.signType,
      paySign: data.paySign,
      success(res) {
        console.log(res)
        // showToast('支付成功');
        resolve();
      },
      fail(err) {
        console.log(err)
        showToast('支付失败');
        // request('Order/delOrder', {
        // 	order_id: order_id,
        // 	type: deltype
        // }).then(res => {

        // })
        uni.navigateTo({
          url: '/pages/home/paystatus?status=3'
        })
      }
    })
  });
}
/**
 * 上传文件
 */
function upload(e) {
  let that = this;
  let token = uni.getStorageSync('user_info').token;
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: api.path + 'common/upload',
      filePath: e,
      name: 'file',
      formData: {
        token: token
      },
      success: (uploadFileRes) => {
        let data = JSON.parse(uploadFileRes.data);
        resolve(data.data);
      },
      fail(err) {
        showToast('图片上传失败');
      }
    });
  });
}
/* 
 微信订阅消息授权
 */
function requestSubscribeMessage() {
  let _this = this;
  request('Common/templateid', {}, 'POST').then(rel => {
    if (rel.length) {
      wx.getSetting({
        withSubscriptions: true,
        success(res) {
          console.log(res)
          let num = 0;
          for (let i = 0; i < rel.length; i++) {
            let id = rel[i];
            if (res.subscriptionsSetting[id] == 'accept') {
              num += 1;
            }
          }
          if (num != rel.length) {
            uni.showModal({
              title: '温馨提示',
              content: '为了更好的促进服务进度，需要您订阅服务通知',
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定');
                  wx.requestSubscribeMessage({
                    tmplIds: rel,
                    success(res) {
                      // console.log(res)
                      // wx.getSetting({
                      // 	withSubscriptions: true,
                      // 	success(res) {
                      // 		console.log(res)
                      // 	}
                      // })
                    },
                    fail(gdx) {
                      console.log(gdx)
                    }
                  })
                } else if (res.cancel) {
                  console.log('用户点击取消');
                }
              }
            });
          }
        }
      })
    }
  })
}
/* 
 微信,支付宝 app支付调取
 */
function appPay(type, data) {
  let payType;
  if (type == 0) {
    payType = 'wxpay';
  } else if (type == 1) {
    payType = 'alipay'
  }
  return new Promise(function(resolve, reject) {
    uni.requestPayment({
      provider: payType,
      orderInfo: data,
      success(res) {
        console.log(res)
        showToast('支付成功');
        resolve();
      },
      fail(err) {
        console.log(err)
        showToast('支付失败');
      }
    })
  });
}
// bgm音乐提示
function voice() {
  const voiceBg = uni.getBackgroundAudioManager();
  request('Common/remind', {}).then(res => {
    voiceBg.src = api.apis + res;
  })
  setInterval(function() {
    // voiceBg.title = '小蜜蜂';
    // voiceBg.play();
    request('Order/getRemind', {}).then(res => {
      console.log(res.status == 1)
      if (res.status == 1) {
        voiceBg.title = '小蜜蜂';
        voiceBg.play();
        request('Order/winRemind', {
          str: res.str
        }).then(gdx => {

        })
      }
    })
  }, 3000)
}

// 拨打电话
function makePhoneCall(e) {
  uni.makePhoneCall({
    phoneNumber: e
  });
}

// 复制到剪切板
function copy(e) {
  uni.setClipboardData({
    data: e,
    success: function() {
      uni.showToast({
        title: '已复制到剪切板'
      })
    }
  });
}

//
function splicing(e, v) {
  let data = '';
  let text = '';
  for (let i = 0; i < e.length; i++) {
    if (i == e.length - 1) {
      data = data + e[i][v];
    } else {
      data = data + e[i][v] + '，';
    }
  }
  return data
}

//
function Phonesplice(e) {
  return e.substr(7, 4)
}

module.exports = {
  request,
  toLogin,
  objToUrl,
  showToast,
  showMsg,
  checkPhone,
  checkName,
  checkDate,
  urlTo,
  backTo,
  urltabTo,
  phoneMask,
  getUserInfo,
  timestampToTime,
  isWeiXin,
  checkEmail,
  checkIDCard,
  getVersion,
  pay,
  appPay,
  upload,
  requestSubscribeMessage,
  voice,
  makePhoneCall,
  seeimg,
  gamerequest,
  api,
  copy,
  splicing,
  Phonesplice,
  loginim,
  msg,
  setMsg
}